package org.jeecg.modules.golf.question.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.golf.question.entity.MyQuestionPlan;
import org.jeecg.modules.golf.question.entity.PlanEffective;
import org.jeecg.modules.golf.question.entity.Question;
import org.jeecg.modules.golf.question.entity.QuestionTypeMapping;
import org.jeecg.modules.golf.question.event.QuestionStatusEvent;
import org.jeecg.modules.golf.question.mapper.MyQuestionPlanMapper;
import org.jeecg.modules.golf.question.mapper.QuestionMapper;
import org.jeecg.modules.golf.question.mapper.QuestionTypeMappingMapper;
import org.jeecg.modules.golf.question.service.IPlanEffectiveService;
import org.jeecg.modules.golf.question.service.IQuestionService;
import org.jeecg.modules.golf.question.service.IQuestionTypeMappingService;
import org.jeecg.modules.golf.tag.entity.TagMapping;
import org.jeecg.modules.golf.tag.event.TagEvent;
import org.jeecg.modules.golf.tag.mapper.TagMappingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * @Description: 问题
 * @Author: jeecg-boot
 * @Date:   2020-11-21
 * @Version: V1.0
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {

	@Autowired
	private QuestionMapper questionMapper;
	@Autowired
	private QuestionTypeMappingMapper questionTypeMappingMapper;
	@Autowired
	private TagMappingMapper tagMappingMapper;
	@Autowired
	private MyQuestionPlanMapper myQuestionPlanMapper;

	@Autowired
	private IPlanEffectiveService planEffectiveService;

	@Autowired
	private ApplicationContext applicationContext;

	@Autowired
	private IQuestionTypeMappingService questionTypeMappingService;



	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Question question, List<QuestionTypeMapping> questionTypeMappingList, List<TagMapping> tagMappingList, List<MyQuestionPlan> myQuestionPlanList) {
		question.setQuestionStatus(0);
		questionMapper.insert(question);
		applicationContext.publishEvent(new QuestionStatusEvent(this,question));
		questionTypeMappingService.save(questionTypeMappingList,1,question.getId());
		applicationContext.publishEvent(new TagEvent(this,question.getId(),tagMappingList,1));

		if(myQuestionPlanList!=null && myQuestionPlanList.size()>0) {
			for(MyQuestionPlan entity:myQuestionPlanList) {
				//外键设置
				entity.setQuestionId(question.getId());
				myQuestionPlanMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Question question,List<QuestionTypeMapping> questionTypeMappingList,List<TagMapping> tagMappingList,List<MyQuestionPlan> myQuestionPlanList) {
		if(myQuestionPlanList!=null && myQuestionPlanList.size()>0) {
		   question.setQuestionStatus(1);
		}
		questionMapper.updateById(question);
		applicationContext.publishEvent(new QuestionStatusEvent(this,question));

		PlanEffective planEffective = question.getPlanEffective();
		planEffective.setId(null);
		planEffective.setQuestionId(question.getId());
		planEffectiveService.savePlanEffective(planEffective);

		//1.先删除子表数据
		questionTypeMappingMapper.deleteByMainId(question.getId());
		tagMappingMapper.deleteByMainId(question.getId());
		myQuestionPlanMapper.deleteByMainId(question.getId());

		//2.子表数据重新插入
		questionTypeMappingService.save(questionTypeMappingList,1,question.getId());

		applicationContext.publishEvent(new TagEvent(this,question.getId(),tagMappingList,1));

		if(myQuestionPlanList!=null && myQuestionPlanList.size()>0) {
			for(MyQuestionPlan entity:myQuestionPlanList) {
				//外键设置
				entity.setQuestionId(question.getId());
				myQuestionPlanMapper.insert(entity);
			}
		}
	}

	/**
	 * 完成某某问题
	 *
	 * @param questionId
	 * @return
	 */
	@Override
	public boolean done(Long questionId) {
		Question source = getById(questionId);
		if(Integer.valueOf(0).equals(source.getQuestionStatus())){
			throw  new JeecgBootException("未解决的问题，不能设置成已解决。");
		}
		Question question = getById(questionId);
		question.setId(questionId);
		question.setQuestionStatus(2);
		question.setTrainee(source.getTrainee());

		applicationContext.publishEvent(new QuestionStatusEvent(this,question));
		return updateById(question);
	}

	@Override
	@Transactional(rollbackFor =Exception.class)
	public void delMain(Long id) {
		questionTypeMappingMapper.deleteByMainId(id);
		tagMappingMapper.deleteByMainId(id);
		myQuestionPlanMapper.deleteByMainId(id);
		questionMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor =Exception.class)
	public void delBatchMain(Collection<Long> idList) {
		for(Long id:idList) {
			questionTypeMappingMapper.deleteByMainId(id);
			tagMappingMapper.deleteByMainId(id);
			myQuestionPlanMapper.deleteByMainId(id);
			questionMapper.deleteById(id);
		}
	}

}
